﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
using MockPrjTeam2.Entity;

namespace MockPrjTeam2.Pages.Forms
{
    public partial class AddTrustRegion : System.Web.UI.Page
    {
        private int trustRegionId;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetCountry();
            }
        }
        private void GetCountry()
        {
            var biz = new CountryBiz();
            DropDownListCountry.DataSource = biz.GetAllCountry();
            DropDownListCountry.DataTextField = "CountryName";
            DropDownListCountry.DataValueField = "CountryID";
            DropDownListCountry.DataBind();
        }

        public int CheckTrustRegionNameExist(string trustRegionName)
        {
            TrustRegionBiz trustRegionBiz = new TrustRegionBiz();
            return trustRegionBiz.CheckNameExist(trustRegionName);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            TrustRegionBiz biz = new TrustRegionBiz();
            TrustRegion trustRegion = new TrustRegion();
            int checkTrustRegionName = CheckTrustRegionNameExist(TxtTrustRegionName.Text);
            if (checkTrustRegionName > 0)
            {
                lblCheckTrustRegionName.Text = "TrustRegion Name does exist ! Please new TrustRegion Name !";
                return;
            }
            trustRegion.TrustRegionID = trustRegionId;
            trustRegion.TrustRegionName = TxtTrustRegionName.Text;
            trustRegion.Description = TxtDescription.Text;
            trustRegion.CountryID = Convert.ToInt32(DropDownListCountry.SelectedValue);

            bool result = biz.AddTrustRegion(trustRegion);
            if (result == true)
            {
                Response.Redirect("../Views/TrustRegionList.aspx");
            }
        }
    }
}